package com.swy.shopgroup.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.swy.shopgroup.R;
import com.swy.shopgroup.Utils;
import com.swy.shopgroup.entity.ShopUser;
import com.swy.shopgroup.network.NetAdress;

public class MeFragment2 extends Fragment {

    private TextView phoneNum, nickEdit, sex;
    private ImageView icon;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_me, container, false);
        initView(v);
        initData();
        return v;
    }

    public void initView(View v) {
        phoneNum = v.findViewById(R.id.phoneNum);
        nickEdit = v.findViewById(R.id.nickEdit);
        sex = v.findViewById(R.id.sex);
        icon = v.findViewById(R.id.icon);
        Button exit = v.findViewById(R.id.exit);
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.setAllInfo(getContext(), "", Utils.shopInfo);
                Utils.setUserToken(getContext(), "");
                Intent intent = new Intent(getContext(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
    }

    public void initData() {
        ShopUser user = Utils.getUserToken(getContext());
        phoneNum.setText(user.getData().getName());
        sex.setText(user.getData().getSex());
        nickEdit.setText(user.getData().getNickname());
        Glide.with(this).load(NetAdress.upload + user.getData().getAvatar()).into(icon);
    }
}
